package com.shaoshaohuo.app.ui;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.SshApplication;
import com.shaoshaohuo.app.constant.TeachPages;
import com.shaoshaohuo.app.framework.SharedPreferencesHelper;
import com.shaoshaohuo.app.framework.SharedPreferencesHelper.Field;
import com.shaoshaohuo.app.utils.DeviceHelper;
import com.shaoshaohuo.app.utils.StringUtil;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

public class SelectDisplayActivity extends BaseActivity {

	private static final double shortHeight = 566;
	private static final double longHeight = 728;

	private RelativeLayout rl_content;
	private RelativeLayout rl_top;
	private RelativeLayout rl_bottom;
	private ImageView iv_top;
	private ImageView iv_bottom;
	private CheckBox cb_top;
	private CheckBox cb_bottom;

	private int halfHeight;
	private int screenWidth;
	private int screenHeight;
	private int height;

	private AnimationDrawable animTop;
	private AnimationDrawable animBottom;
	private ImageView ivTeachSelectRole;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_display);
		initView();
		initLayout();
		setupView();
		initData();
		initAnim();
		
		initTeachPage();
	}

	/**
	 * 教程页面
	 */
	private void initTeachPage() {
		if (StringUtil.isEmpty(SharedPreferencesHelper.getString(TeachPages.PAGE_SELECT_DISPLAY, ""))) {
			//显示教程遮罩
			ivTeachSelectRole.setVisibility(View.VISIBLE);
		}
	}

	private void initView() {
		rl_content = (RelativeLayout) findViewById(R.id.rl_content);
		rl_top = (RelativeLayout) findViewById(R.id.rl_top);
		rl_bottom = (RelativeLayout) findViewById(R.id.rl_bottom);
		iv_top = (ImageView) findViewById(R.id.iv_top);
		iv_bottom = (ImageView) findViewById(R.id.iv_bottom);
		cb_top = (CheckBox) findViewById(R.id.cb_top);
		cb_bottom = (CheckBox) findViewById(R.id.cb_bottom);
		ivTeachSelectRole = (ImageView) findViewById(R.id.iv_teach_select_role);
		ivTeachSelectRole.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ivTeachSelectRole.setVisibility(View.GONE);
				SharedPreferencesHelper.setString(TeachPages.PAGE_SELECT_DISPLAY, "true");
			}
		});
	}

	private void initLayout() {
		screenWidth = DeviceHelper.getScreenWidth(this);
		int statusBarHeight = DeviceHelper.getStatusBarHeight(this);
		screenHeight = DeviceHelper.getScreenHeight(this) - statusBarHeight;
		halfHeight = (int) (screenHeight * (longHeight - shortHeight) / (shortHeight + longHeight) / 2 + 0.5);
		height = (int) (screenHeight * longHeight / (shortHeight + longHeight)) + 2;

		LayoutParams topParams = (LayoutParams) rl_top.getLayoutParams();
		topParams.height = height;
		rl_top.setLayoutParams(topParams);

		LayoutParams bottomParams = (LayoutParams) rl_bottom.getLayoutParams();
		bottomParams.height = height;
		rl_bottom.setLayoutParams(bottomParams);

		LayoutParams ivTopParams = (LayoutParams) iv_top.getLayoutParams();
		ivTopParams.setMargins(ivTopParams.leftMargin, ivTopParams.topMargin, 0, ivTopParams.topMargin + halfHeight);
		iv_top.setLayoutParams(ivTopParams);

		LayoutParams ivBottomParams = (LayoutParams) iv_bottom.getLayoutParams();
		ivBottomParams.setMargins(0, ivBottomParams.rightMargin + halfHeight, ivBottomParams.rightMargin,
				ivBottomParams.bottomMargin);
		iv_bottom.setLayoutParams(ivBottomParams);
	}

	private void setupView() {
		rl_content.setEnabled(true);
		rl_content.setOnTouchListener(new RelativeTouchListener());

		CheckBoxClick checkBoxClick = new CheckBoxClick();
		cb_top.setOnCheckedChangeListener(checkBoxClick);
		cb_bottom.setOnCheckedChangeListener(checkBoxClick);
	}

	private void initData() {
		int defaultDisplay = SharedPreferencesHelper.getInt(Field.DEFAULT_DISPLAY, 0);
		if (defaultDisplay == 1) {// 默认显示物流
			cb_top.setChecked(true);
		} else if (defaultDisplay == 2) {// 默认显示电商
			cb_bottom.setChecked(true);
		}
	}

	private void initAnim() {
		animTop = (AnimationDrawable) rl_top.getBackground();
		animBottom = (AnimationDrawable) rl_bottom.getBackground();
		animTop.start();
		animBottom.start();
	}

	class CheckBoxClick implements OnCheckedChangeListener {
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			if (isChecked == true) {
				if (buttonView.getId() == R.id.cb_top) {
					cb_bottom.setChecked(false);
				} else {
					cb_top.setChecked(false);
				}
			}

		}
	}

	enum DownMotion {
		TOP, BOTTOM;
	}

	class RelativeTouchListener implements OnTouchListener {
		DownMotion downMotion;

		@Override
		public boolean onTouch(View v, MotionEvent event) {

			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				float downX = event.getX();
				float downY = event.getY();
				float midY = (float) (-2.0 * halfHeight / screenWidth * downX + height);
				if (midY < downY) {
					downMotion = DownMotion.BOTTOM;
				} else {
					downMotion = DownMotion.TOP;
				}

				break;
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_CANCEL:
				float upX = event.getX();
				float upY = event.getY();
				// = -2 * halfHeight / screenWidth * upX + screenHeight / 2 +
				// halfHeight * 2;
				float y = (float) (-halfHeight * 2.0 / screenWidth * upX + height);
				DownMotion upMotion;
				if (y < upY) {
					upMotion = DownMotion.BOTTOM;
				} else {
					upMotion = DownMotion.TOP;
				}
				if (upMotion == downMotion) {
					Intent intent = new Intent(SelectDisplayActivity.this, MainTabActivity.class);
					switch (downMotion) {
					case BOTTOM:
						SshApplication.main_state = 2;
						break;
					case TOP:
						SshApplication.main_state = 1;
						break;
					}
					if (cb_top.isChecked()) {
						SharedPreferencesHelper.setInt(Field.DEFAULT_DISPLAY, 1);
					}
					if (cb_bottom.isChecked()) {
						SharedPreferencesHelper.setInt(Field.DEFAULT_DISPLAY, 2);
					}
					startActivity(intent);
					finish();
				}
				break;

			}
			return true;

		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}
